package com.ct.spring.struts.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.ct.spring.mybatis.model.Student;
import com.ct.spring.mybatis.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import com.ct.spring.mybatis.model.Student;

import com.ct.spring.mybatis.model.Page;


/**
 * 用户控制器
 * @author Administrator
 *
 */
public class StudentAction extends ActionSupport implements ModelDriven<Object>{

	private static final long serialVersionUID = 1L;
	
	/**
	 * 注入用户业务服务
	 */
	@Autowired
	private StudentService studentservice;
	/**
	 * 用户对象参数
	 */
	private Student student;
	
	/**
	 * 每页大小参数
	 */
	private int pageSize;
	/**
	 * 当前页码参数
	 */
	private int pageNum;
	/**
	 * 返回分页数据对象
	 */
	private Page page;
	
	public StudentService getStudentservice() {
		return studentservice;
	}
	public void setStudentservice(StudentService studentservice) {
		this.studentservice = studentservice;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	/**
	 * 模糊查询用户列表
	 * 
	 * @return
	 */
	public String queryLikeStudents(){
		Map<String,Object> cond=new HashMap<String,Object>();
		System.out.println("pageSize="+pageSize);
		System.out.println("pageNum="+pageNum);
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("student", student);
		System.out.println("sname="+student.getSname());
		page = studentservice.queryLikeStudents(cond);
		return "success";
	}
	/**
	 * 查询单个用户详细信息
	 * 
	 * @return
	 */
	public String getStudent(int sid){
		student = studentservice.getStudent(sid);
        ServletActionContext.getRequest().setAttribute("data", student);
		return "success";
	}
	
	/**
	 * 增加新用户
	 * 
	 * @return
	 */
	public String addStudent(){
		studentservice.addStudent(student);
		return "success";
	}
	
	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public String delStudent(){
		studentservice.delStudent(student);
		return "success";
	}
	
	/**
	 * 编辑用户
	 * 
	 * @return
	 */
	public String updateStudent(){
		studentservice.updateStudent(student);
		return "success";
	}
	
	
	@Override
	public Student getModel() {
		if(student==null){
			student = new Student();
		}
		return student;
	}
	
}
